import path from 'path';
import fs from 'fs';
import {pathToFileURL} from 'url';
//递归遍历文件夹,找到_router.js结尾的文件
const readDir = (dir, app) => {
  const files = fs.readdirSync(dir);
  files.forEach(async file => {
    let filePath = path.resolve(dir, file);
    const stats = fs.statSync(filePath);
    if (stats.isDirectory()) {
      readDir(filePath, app);
    } else {
      if (file.startsWith('_')) {
        filePath = pathToFileURL(filePath);
        const {default: router} = await import(filePath);
        if(!router) return;
        app.use(router.routes()).use(router.allowedMethods());
      }
    }
  })
}
export default readDir;

